package com.hk.rabbitmq.six;

import com.hk.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.*;
import org.apache.commons.io.FileUtils;

import java.io.File;


/**
 * @author: dpb
 * @date: 2022/3/22
 */
public class ReceiveDirect01 {
    private static final String EXCHANGE_NAME = "direct logs";
    public static void main(String[] args) throws Exception{

        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        String queueName = "disk";
        channel.queueDeclare(queueName,false,false,false,null);
        //绑定
        channel.queueBind(queueName,EXCHANGE_NAME,"error");
        System.out.println("等待接收消息......");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody(),"UTF-8");
            msg = "接收绑定主键为：" + message.getEnvelope().getRoutingKey() + "，消息为：" + msg;
            File file = new File("D:\\msg.txt");
            FileUtils.writeStringToFile(file, msg, "UTF-8");
            System.out.println("错误日志已经接收");
        };
        channel.basicConsume(queueName, true, deliverCallback, cancelCallback -> {
        });
    }
}
